package cn.huapu.power.energy.util.python;
//
import java.io.*;
import java.util.regex.*;
//
public class PythonTool {	

	/// 在java中直接调用python脚本，不传入参数：
	public static void runPythonFile(String pythonFileFullName){
        Process proc;
        try {
            proc = Runtime.getRuntime().exec("python "+pythonFileFullName);
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),"utf-8"));
            ///【输出调试信息】
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
                //System.out.println(unicodeToString(line));
            }
            in.close();
            proc.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } 
	}
///-------------------------------------------------------
	//
	/// 在java中直接调用python脚本，并传入参数：
	public static void runPythonFile(String pythonFileFullName,String Params){
        Process proc;
        try {
        	String[] args = new String[] { "python", pythonFileFullName, Params}; 
        	proc=Runtime.getRuntime().exec(args);
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),"utf-8"));
            ///【输出调试信息】
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
                //System.out.println(unicodeToString(line));
            }
            in.close();
            proc.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } 
	}
//========================================================
	/// unicodeToString
	/// 【通用码-转-Java字符串】
	private static String unicodeToString(String str) { 	
		Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");	
		Matcher matcher = pattern.matcher(str);	
		char ch;	
		while (matcher.find()) {
			String group = matcher.group(2);
			ch = (char) Integer.parseInt(group, 16);
			String group1 = matcher.group(1);		
			str = str.replace(group1, ch + "");	
			}	
		return str;
	}
///-------------------------------------------------------
	/// stringToUnicode
	/// 【Java字符串-转-通用码】
	private static String stringToUnicode(String s) {	
		try {		
			StringBuffer out = new StringBuffer("");		
			//直接获取字符串的unicode二进制		
			byte[] bytes = s.getBytes("unicode");		
			//然后将其byte转换成对应的16进制表示即可		
			for (int i = 0; i < bytes.length - 1; i += 2) {			
				out.append("\\u");			
				String str = Integer.toHexString(bytes[i + 1] & 0xff);			
				for (int j = str.length(); j < 2; j++) {				
					out.append("0");			
					}			
				String str1 = Integer.toHexString(bytes[i] & 0xff);			
				out.append(str1);			
				out.append(str);		
			}		
			return out.toString();	
		} 
		catch (UnsupportedEncodingException e) {		
			e.printStackTrace();		
			return null;	
		}
	}
//
}
